/**
 * Program   :   UpdateBaseAction.java
 * Author    :   shadow guo
 * Create    :   2013-7-6 下午8:56:06
 *
 * Copyright 2008 by Tongfu Information Technology Co. Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of Tongfu Information Technology Co. Ltd. ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with Tongfu Information Technology Co. Ltd.
 * 
 */
package com.tongfusoft.web.core.action;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.tongfusoft.exception.ServiceException;
import com.tongfusoft.utils.web.WebUtils;
import com.tongfusoft.web.core.Constant;
import com.tongfusoft.web.core.data.BaseData;
import com.tongfusoft.web.core.data.User;
import com.tongfusoft.web.core.service.IUpdateService;

public class UpdateAction extends BaseAction{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -6968853285157852293L;

	/**
	 * 实体bean名称，包名（最后一层）+名称
	 */
	protected String beanName;
	
	/**
	 * 执行结果
	 */
	protected boolean result;
	
	/**
	 * 返回的提示信息
	 */
	protected String message;
	
	private IUpdateService updateService;
	

	public IUpdateService getUpdateService() {
		return updateService;
	}

	public void setUpdateService(IUpdateService updateService) {
		this.updateService = updateService;
	}

	public String getBeanName() {
		return beanName;
	}

	public void setBeanName(String beanName) {
		this.beanName = beanName;
	}

	public boolean isResult() {
		return result;
	}

	public void setResult(boolean result) {
		this.result = result;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	public Map convertParam(){
		Map params = WebUtils.getRequestParameters(getRequest());
		User user = (User)super.getSession().getAttribute(Constant.SESSION_USER);
		params = this.convertMap(params);
		params.put(BaseData.FIELD_LASTUPDBY, user.getPkValue());
		params.put(BaseData.FIELD_LASTUPDDATE, new Date());
		
		return params;
	}
	
	public List<Map> convertListParam(){
		return null;
	}
	
	
	/**
	 * 更新
	 * @return
	 */
	public String update(){
		try{
			updateService.update(getDataDefine(this.getBeanName()),this.convertParam());
			result = true;
			message = super.getText("common.prompt.success");
		}catch(Exception e){
			LOG.error(e.getMessage(),e);
			result = false;
			
			if(e instanceof ServiceException && StringUtils.isNotEmpty(((ServiceException)e).getErrMsgCode())){
				message = super.getText(((ServiceException)e).getErrMsgCode());
			}else{
				message = super.getText("common.prompt.fail");
			}
		}
		
		return SUCCESS;
	}
	
	/**
	 * 批量更新
	 * @return
	 */
	public String updateList(){
		try{
			updateService.update(getDataDefine(this.getBeanName()),this.convertListParam());
			result = true;
			message = super.getText("common.prompt.success");
		}catch(Exception e){
			LOG.error(e.getMessage(),e);
			result = false;
			
			if(e instanceof ServiceException && StringUtils.isNotEmpty(((ServiceException)e).getErrMsgCode())){
				message = super.getText(((ServiceException)e).getErrMsgCode());
			}else{
				message = super.getText("common.prompt.fail");
			}
		}
		
		return SUCCESS;
	}
	
	
}
